package com.android.kmlmrg.services.rest.method;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONObject;

import com.android.kmlmrg.R;
import com.android.kmlmrg.event.RestGetUserEvent;
import com.android.kmlmrg.model.ApiSession;
import com.android.kmlmrg.services.ServiceController;

import android.app.Application;
import android.os.AsyncTask;
import android.util.Log;


public class Logout extends AsyncTask<Void, Integer, String> {


	/**LOG**/
	private static final  String LOG_TAG = Logout.class.getSimpleName();

	private Application application;

	private int code;

	private static final String METHODE_NAME = "/logout";

	public Logout (Application pApplication) {
		application = pApplication;
	}

	@Override
	protected String doInBackground(Void... arg0) {
		try {
			String vUrl = application.getString(R.string.user_endpoint) + METHODE_NAME ;

			ApiSession vApiSession =  ServiceController.getInstance().getApiSession() ;
			JSONObject vJsonObject = new JSONObject();				
			vJsonObject.put(ApiSession.WSCALLERID, vApiSession.getWsCallerId());
			vJsonObject.put(ApiSession.TIMESTAMP, vApiSession.getTimesTamp());
			vJsonObject.put(ApiSession.HASH, vApiSession.getHash());
			String message = vJsonObject.toString();

			URLConnection url = new URL(vUrl).openConnection();
			HttpURLConnection httpcon = (HttpURLConnection) ((new URL(vUrl).openConnection()));

			httpcon.setRequestProperty("Content-Type", "application/json");
			httpcon.setRequestProperty("Accept", "application/json");
			httpcon.setRequestMethod("POST");

			httpcon.setDoInput(true);
			httpcon.setDoOutput(true);
			httpcon.setFixedLengthStreamingMode(message.getBytes().length);
			httpcon.connect();


			OutputStream os = new BufferedOutputStream(httpcon.getOutputStream());
			os.write(message.getBytes());
			//clean up
			os.flush();


			// this will be useful so that you can show a typical 0-100% progress bar
			int fileLength = httpcon.getContentLength();

			code = httpcon.getResponseCode();

			// download the file

			InputStream input = httpcon.getInputStream();
			BufferedReader r = new BufferedReader(new InputStreamReader(input));
			StringBuilder total = new StringBuilder();
			String line;
			while ((line = r.readLine()) != null) {
				total.append(line);
			}
			os.flush();
			os.close();
			input.close();	

			return total.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		Log.i(LOG_TAG, "START - onPostExecute(String result)");
		Log.i(LOG_TAG, "RESPONSE : " + result);
		ServiceController.getInstance().setAuthentication(false);
		Log.i(LOG_TAG, "END - onPostExecute");
		super.onPostExecute(result);
	}


}
